package com.example.lifecycledemo;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

// 使用了 Lifecycle 之后，只需要将观察者与被观察者绑定即可
public class MainActivity1 extends AppCompatActivity {

    MyLocationListener myLocationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myLocationListener = new MyLocationListener(this,
                new MyLocationListener.OnLocationChangedListener() {
            @Override
            public void onChange(double latitude, double longitude) {
                // 位置发生变化时，调用该方法
            }
        });

        // 将观察者（MyLocationListener）与被观察者（MainActivity1）绑定
        getLifecycle().addObserver(myLocationListener);

        Fragment fragment = new Fragment();
    }

}